package cn.tangcent.rules;

import cn.tangcent.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by TangMing on 2017/4/8.
 * 规则:每列中每个数字必出现一次
 * 如果每列中某个数字只能出现在某格，则此格中数字可确定
 */
public class EleRule implements SudokuRule {

    @Override
    public boolean check(SudokuData sudokuData) {
        Holder<Boolean> resultHolder = new Holder<>(false);
        Coordinate[] rests = sudokuData.rests().stream().map(SudokuUtils::index2XY).toArray(Coordinate[]::new);
        Integer[] optionals = sudokuData.optionals(rests);
        for (int i = 0; i < optionals.length; i++) {
            if (SudokuUtils.single(optionals[i])) {
                resultHolder.setTarget(true);
                int val = SudokuUtils.unbox(optionals[i]);
                sudokuData.sure(new Value(val, rests[i]));
            }
        }
        return resultHolder.getTarget();
    }
}
